<?php
$thisID = $_POST["thisID"];
$indexCourse = $_POST["indexCourse"];
$thidIDArray = explode("_",$thisID);
$dayIndex = str_replace("d","",$thidIDArray[0]) ;
$indexProgram = $_POST["indexProgram"];
$indexResource = $_POST["indexResource"];
$action = $_POST["action"];
$colResourcelink = $_POST["colResourcelink"];
$colResourcelinkname = $_POST["colResourcelinkname"];
$program_select = $_POST["program_select"];
$program_name = $_POST["program_name"];
$program_starttime = $_POST["program_starttime"];
$program_endtime = $_POST["program_endtime"];
$site = $_POST["site"];
$googleurl = $_POST["googleurl"];
$attachurlcount = $_POST["attachurlcount"];
$description = $_POST["description"];
$category = $_POST["category"];
$pricerange = $_POST["pricerange"];
$creditecard = $_POST["creditecard"];
$address = $_POST["address"];
$phone = $_POST["phone"];
$hotel_star = $_POST["hotel_star"];
$hotel_room = $_POST["hotel_room"];
$transportation_company = $_POST["transportation_company"];

$colResourcelinkArray = explode(",",$colResourcelink);
$colResourcelinknameArray = explode(",",$colResourcelinkname);

require("dbinit.php");

$query = "SELECT * from ucCrouseday WHERE indexProgram=" . $indexProgram;
$result = mysql_query($query, $link);

if ($result || $action =="edit") {
	// edit program
	$query = "UPDATE ucProgramschedule SET colOrder=0, colTimestart='$program_starttime', colTimeend='$program_endtime' WHERE indexProgram=$indexProgram";
	$result = mysql_query($query, $link);
	$query = "SELECT colType FROM ucProgramresources WHERE indexResource=$indexResource";
	$result = mysql_query($query, $link);
	$row = mysql_fetch_row($result);
	//echo $row[0] . "/" . $program_select . "/" . ($row[0] == $program_select);
	if ($row[0] == $program_select) {
	switch ($program_select) {
		case "interest":
			$query = "UPDATE tiInterestInformation SET colInterestname='$program_name', colInterestdescription='$description', colInterestsite='$site', colInterestpositiongoogle='$googleurl' WHERE indexResource=$indexResource";
			break;
		case "food":
			$query = "UPDATE tiFoodinformation SET colFoodname='$program_name', colFoodcategory='$category', colFoodpricerange='$pricerange', colFooddescription='$description', colFoodsite='$site', colFoodpositiongoogle='$googleurl', colFoodaddress='$address', colFoodtelephone='$phone', colFoodcreditecard='$creditecard' WHERE indexResource=$indexResource";
			break;
		case "activity":
			$query = "UPDATE tiActivityinformation SET colActivityname='$program_name', colActivitydescription='$description', colActivitysite='$site', colActivityaddress='$address', colActivitypositiongoogle='$googleurl', colActivitycategory='$category' WHERE indexResource=$indexResource";
			break;
		case "hotel":
			$query = "UPDATE tiHotelinformation SET colHotelname='$program_name', colHotelpositiongoogle='$googleurl', colHoteldescription='$description', colHotelsite='$site', colHotelcategory='$category', colHotelstar='$hotel_star', colHotelroomtype='$hotel_room', colHotelprice='$pricerange', colHoteladdress='$address', colHoteltelephone='$phone', colHotelcreditecard='$creditecard' WHERE indexResource=$indexResource";
			break;
		case "transportation":
			$query = "UPDATE tiTransportationInformation SET colTransportationname='$program_name', colTransportationcategory='$category', colTransportationcompany='$transportation_company', colTransportationsite='$site', colTransportationdescription='$description' WHERE indexResource=$indexResource";
			break;
		case "schedule":
			$query = "UPDATE tiScheduleinformation SET colSchedulename='$program_name', colScheduledescription='$description' WHERE indexResource=$indexResource";
		} //end switch
		$result = mysql_query($query, $link);
		$query = "DELECT FROM tiResourcelink WHERE indexResource=$indexResource";
		$result = mysql_query($query, $link);
	} else {
	$query = "DELETE FROM ucProgramresources WHERE indexResource=$indexResource";
	$result = mysql_query($query, $link);
	$query = "INSERT INTO ucProgramresources (indexProgram, colType) VALUES ($indexProgram,'$program_select')";
	$result = mysql_query($query, $link);
	$indexResource = mysql_insert_id();
	switch ($program_select) {
		case "interest":
			$query = "INSERT INTO tiInterestInformation (indexResource, colInterestname, colInterestdescription, colInterestsite, colInterestpositiongoogle) ";
			$query .= " VALUES ($indexResource, '$program_name', '$description', '$site', '$googleurl')";
			break;
		case "food":
			$query = "INSERT INTO tiFoodinformation (indexResource, colFoodname, colFoodcategory, colFoodpricerange, colFooddescription, colFoodsite, colFoodpositiongoogle, colFoodaddress, colFoodtelephone, colFoodcreditecard) ";
			$query .= " VALUES ($indexResource, '$program_name', '$category', '$pricerange', '$description', '$site', '$googleurl', '$address', '$phone', '$creditecard')";
			break;
		case "activity":
			$query = "INSERT INTO tiActivityinformation (indexResource, colActivityname, colActivitydescription, colActivitysite, colActivityaddress, colActivitypositiongoogle, colActivitycategory) ";
			$query .= " VALUES ($indexResource, '$program_name', '$description', '$site', '$address', '$googleurl', '$category')";
			break;
		case "hotel":
			$query = "INSERT INTO tiHotelinformation (indexResource, colHotelname, colHotelpositiongoogle, colHoteldescription, colHotelsite, colHotelcategory, colHotelstar, colHotelroomtype, colHotelprice, colHoteladdress, colHoteltelephone, colHotelcreditecard) ";
			$query .= " VALUES ($indexResource, '$program_name, '$googleurl', '$description', '$site', '$category', $hotel_star, '$hotel_room', '$pricerange', '$address', '$phone', '$creditecard')";
			break;
		case "transportation":
			$query = "INSERT INTO tiTransportationInformation (indexResource, colTransportationname, colTransportationcategory, colTransportationcompany, colTransportationsite, colTransportationdescription) ";
			$query .= " VALUES ($indexResource, '$program_name', '$category', '$transportation_company', '$site', '$description')";
			break;
		case "schedule":
			$query = "INSERT INTO tiScheduleinformation (indexResource, colSchedulename, colScheduledescription) ";
			$query .= " VALUES ($indexResource, '$program_name', '$description')";
		} //end switch
	$result = mysql_query($query, $link);

	//if (strlen($colResourcelink) > 0) {

		} //end else if
		if (count($colResourcelinkArray) == count($colResourcelinknameArray) || count($colResourcelinknameArray) == $attachurlcount) {
			for ($i=0;$i<$attachurlcount;$i++) {
				$query = "INSERT INTO tiResourcelink (indexResource, colResourcelink, colResourcelinkname) VALUES ($indexResource,'$colResourcelinkArray[$i]','$colResourcelinknameArray[$i]')";
				$result = mysql_query($query, $link);
			} //end for
		} //end if
	//}
	echo "true"  ;

} else {
	// new program
	$query = "INSERT INTO ucCrouseday (indexCourse, colDay) VALUES ($indexCourse,$dayIndex)";
	$result = mysql_query($query, $link);
	$indexProgram = mysql_insert_id();
	$query = "INSERT INTO ucProgramresources (indexProgram, colType) VALUES ($indexProgram,'$program_select')";
	$result = mysql_query($query, $link);
	$indexResource = mysql_insert_id();
	$query = "INSERT INTO ucProgramschedule (indexProgram, colOrder, colTimestart, colTimeend) VALUES ($indexProgram,0,'$program_starttime','$program_endtime')";
	$result = mysql_query($query, $link);
	switch ($program_select) {
		case "interest":
			$query = "INSERT INTO tiInterestInformation (indexResource, colInterestname, colInterestdescription, colInterestsite, colInterestpositiongoogle) ";
			$query .= " VALUES ($indexResource, '$program_name', '$description', '$site', '$googleurl')";
			break;
		case "food":
			$query = "INSERT INTO tiFoodinformation (indexResource, colFoodname, colFoodcategory, colFoodpricerange, colFooddescription, colFoodsite, colFoodpositiongoogle, colFoodaddress, colFoodtelephone, colFoodcreditecard) ";
			$query .= " VALUES ($indexResource, '$program_name', '$category', '$pricerange', '$description', '$site', '$googleurl', '$address', '$phone', '$creditecard')";
			break;
		case "activity":
			$query = "INSERT INTO tiActivityinformation (indexResource, colActivityname, colActivitydescription, colActivitysite, colActivityaddress, colActivitypositiongoogle, colActivitycategory) ";
			$query .= " VALUES ($indexResource, '$program_name', '$description', '$site', '$address', '$googleurl', '$category')";
			break;
		case "hotel":
			$query = "INSERT INTO tiHotelinformation (indexResource, colHotelname, colHotelpositiongoogle, colHoteldescription, colHotelsite, colHotelcategory, colHotelstar, colHotelroomtype, colHotelprice, colHoteladdress, colHoteltelephone, colHotelcreditecard) ";
			$query .= " VALUES ($indexResource, '$program_name', '$googleurl', '$description', '$site', '$category', $hotel_star, '$hotel_room', '$pricerange', '$address', '$phone', '$creditecard')";
			break;
		case "transportation":
			$query = "INSERT INTO tiTransportationInformation (indexResource, colTransportationname, colTransportationcategory, colTransportationcompany, colTransportationsite, colTransportationdescription) ";
			$query .= " VALUES ($indexResource, '$program_name', '$category', '$transportation_company', '$site', '$description')";
			break;
		case "schedule":
			$query = "INSERT INTO tiScheduleinformation (indexResource, colSchedulename, colScheduledescription) ";
			$query .= " VALUES ($indexResource, '$program_name', '$description')";
	} //end switch
	$result = mysql_query($query, $link);

		if (count($colResourcelinkArray) == count($colResourcelinknameArray) || count($colResourcelinknameArray) == $attachurlcount) {
			for ($i=0;$i<$attachurlcount;$i++) {
				$query = "INSERT INTO tiResourcelink (indexResource, colResourcelink, colResourcelinkname) VALUES ($indexResource,'$colResourcelinkArray[$i]','$colResourcelinknameArray[$i]')";
				$result = mysql_query($query, $link);
			}
		}
	echo "true" ;	
}


?>